package com.crcchem.practice.demos.web.controller;

import com.crcchem.practice.demos.web.entity.User;
import com.crcchem.practice.demos.web.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @projectName: practice
 * @package: com.crcchem.practice.demos.web.controller
 * @className: UserController
 * @author: 李富强
 * @description: TODO
 * @date: 2025/5/15 15:08
 * @version: 1.0
 */
@Controller
@Slf4j
@RequestMapping("/practice/user")
public class UserController {

    @Autowired
    private UserService userService;


    @RequestMapping("/userList")
    public String queryUserList(Model model,String name,Integer pageNumber) {
        //测试git提交或者控制问题
        Integer pageSize = 10;
        List<User> usersize = userService.queryUserListAll();
        int totalCount = usersize.size();
        int totalPages = (totalCount + pageSize - 1) / pageSize; // 计算总页数
        pageNumber = ((pageNumber == null ? 1 : pageNumber)-1)*pageSize;
        List<User> userList = userService.queryUserList(name, pageNumber, pageSize);
        model.addAttribute("users", userList);
        model.addAttribute("usercount", totalCount);
        model.addAttribute("currentPage", pageNumber);
        model.addAttribute("totalPages", totalPages);
        model.addAttribute("name", name); // 保留查询条件
        return "index";
    }
}
